package com.yiwei.hdb.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.api.callback.LoanResultContract;
import com.yiwei.hdb.api.requestInfo.RepayResultRequest;
import com.yiwei.hdb.constant.Const;
import com.yiwei.hdb.event.OnHomeEventMsg;
import com.yiwei.hdb.model.LoanResultModel;
import com.yiwei.hdb.util.Util;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

/**
 * 还钱结果页
 */
public class RepayResultActivity extends BaseActivity
        implements OnClickListener, LoanResultContract.View {
    private TextView btn;
    private String deal_id, repayMoney, repayOverdue;
    private LinearLayout successView, failView, timeView;
    private TextView money;
    private TextView overdue;
    private TextView time;
    private TextView tishi;
    private TextView title, repayText;
    private int type;
    private RepayResultRequest api;

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setBaseContentView(R.layout.repay_result_activity);
        baseInitView(this, "PayResult_Page", Const.ViewType.ACTIVITY);
        initView();
    }

    private void initView() {
        api = RepayResultRequest.create();
        api.attachView(this);
        type = getIntent().getIntExtra("type", 0);
        deal_id = getIntent().getStringExtra("deal_id");
        repayMoney = getIntent().getStringExtra("money");
        repayOverdue = getIntent().getStringExtra("overdue");
        if (repayMoney.contains("￥")) repayMoney = repayMoney.replace("￥", "");
        if (repayOverdue.contains("￥")) repayOverdue = repayOverdue.replace("￥", "");


        setTitleText(getResources().getString(R.string.repay_result));
        getLeftImgBtn().setVisibility(View.GONE);
        setBackIntent();
        money = ((TextView) findViewById(R.id.repay_money));
        overdue = ((TextView) findViewById(R.id.overdue));
        time = ((TextView) findViewById(R.id.time));
        title = ((TextView) findViewById(R.id.title));
        repayText = ((TextView) findViewById(R.id.repay_text));
        tishi = ((TextView) findViewById(R.id.repay_tishi));
        successView = ((LinearLayout) findViewById(R.id.success_view));
        failView = ((LinearLayout) findViewById(R.id.fail_view));
        timeView = ((LinearLayout) findViewById(R.id.time_view));
        btn = ((TextView) findViewById(R.id.finish_btn));
        btn.setOnClickListener(this);
        ((TextView) findViewById(R.id.wait)).setOnClickListener(this);
        ((TextView) findViewById(R.id.try_again)).setOnClickListener(this);
        if (type == 1) {
            title.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.default_orange_color));
            title.setText(getResources().getString(R.string.repay_success));
            tishi.setText(getResources().getString(R.string.repay_tishi_success));
            overdue.setText(repayOverdue);
            btn.setVisibility(View.VISIBLE);
            successView.setVisibility(View.VISIBLE);
            failView.setVisibility(View.GONE);
            timeView.setVisibility(View.VISIBLE);
        } else if (type == 3) {
            setTitleText(getResources().getString(R.string.offline_payment));
            title.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.default_orange_color));
            title.setText(getResources().getString(R.string.subbmit_success));
            overdue.setText(repayOverdue);
            btn.setVisibility(View.VISIBLE);
            successView.setVisibility(View.VISIBLE);
            failView.setVisibility(View.GONE);
            timeView.setVisibility(View.VISIBLE);
            repayText.setText(getString(R.string.subimt_time));
            time.setText(Util.timeStamp2Date(System.currentTimeMillis() / 1000 + "", ""));
            tishi.setText(getString(R.string.repay_result_tishi));
        } else {
            title.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.default_gray_color));
            title.setText(getResources().getString(R.string.repay_fail));
            tishi.setText(getResources().getString(R.string.repay_tishi_fail));
            time.setText("");
            overdue.setText(repayOverdue);
            btn.setVisibility(View.GONE);
            successView.setVisibility(View.GONE);
            failView.setVisibility(View.VISIBLE);
            timeView.setVisibility(View.GONE);
        }

        money.setText(repayMoney);

        if (type != 3)
            getLoanResult();
    }


    /**
     * 更新用户中心数据
     */
    private void updateUserBroadcast() {

        Map<String, String> map = new HashMap<>();
        map.put("type", Const.SendType.UPDATE);
        EventBus.getDefault().post(new OnHomeEventMsg(map));
    }

    /**
     * 更新还钱页数据
     */
    private void updateRepayBroadcast() {

        Map<String, String> map = new HashMap<>();
        map.put("type", Const.SendType.UPDATE_REPAY);
        EventBus.getDefault().post(new OnHomeEventMsg(map));
    }

    /**
     * 切换主界面
     *
     * @param currentIndex
     */
    private void sendEventBus(int currentIndex) {

        Map<String, String> map = new HashMap<>();
        map.put("type", Const.SendType.SWITCH_PAGE);
        map.put("currentIndex", currentIndex + "");
        EventBus.getDefault().post(new OnHomeEventMsg(map));

        Util.startActivity(this, MainActivityNew.class);
        finish();
    }

    public void onClick(View paramView) {
        switch (paramView.getId()) {
            default:
                return;
            case R.id.finish_btn:
            case R.id.wait:
                sendEventBus(Const.View.HOME_VIEW);
                return;
            case R.id.try_again:
                sendEventBus(Const.View.REPAY_VIEW);
                break;
        }
    }


    /**
     * 还钱信息
     */
    private void getLoanResult() {
        api.getLoanResultInfo(deal_id);
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {

    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void getLoanResultInfoSuccess(LoanResultModel model) {
        if (model.getStatus() == 3) {
            setTitleText(model.getData().getTitle());
            title.setText(model.getData().getKeyword());
            if (model.getData().getRepay_money() <= 0) money.setText(repayMoney);
            else
                money.setText(model.getData().getRepay_money() + "");
            if (model.getData().getImpose_money() <= 0) overdue.setText(repayOverdue);
            else
                overdue.setText(model.getData().getImpose_money() + "");
            time.setText(model.getData().getRepay_time());
            tishi.setText(model.getData().getContent());

            updateUserBroadcast();
            updateRepayBroadcast();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        api = null;
    }
}
